<?php
namespace app\tadmin\controller;
/**
 * kindeditor 编辑器管理
 */
class Editor extends Controller{

    /**
     * 上传文件 * 加水印
     */
    public function upload(){
        $size = 1024*1024*9;//不得大于9M
        $ext = 'gif,jpg,jpeg,png,bmp,swf,flv,swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,doc,docx,xls,xlsx,ppt,txt,zip,rar,gz,bz2';
        $file = request()->file('imgFile');
        $uploadPath = realpath(WEB_PATH) . '/uploads';
        $uploadUrl  = config('myconfig.host_url') . 'uploads/';
        $info = $file->validate(['size'=>$size,'ext'=>$ext])->move( $uploadPath );
        if($info){
            $url = $uploadUrl . $info->getSaveName();
            $uploadFile = $uploadPath . '/' . $info->getSaveName();
            //添加水印
            //$image = \think\Image::open($uploadFile);
            //$image->water(WEB_PATH.'assets/water.png',\think\Image::WATER_SOUTHEAST )->save($uploadFile);
            
            if(strpos($url , "\\") > 0){
                $url = str_replace("\\" , '/' , $url);
            }
            return json(['error' => 0, 'url' => $url]);
        }else{
            return json(['error' => 1, 'message' => $file->getError()]);
        }
    }
    /**
     * 上传文件
     */
    public function uploadn(){
        $size = 1024*1024*9;//不得大于9M
        $ext = 'gif,jpg,jpeg,png,bmp,swf,flv,swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,doc,docx,xls,xlsx,ppt,txt,zip,rar,gz,bz2';
        $file = request()->file('imgFile');
        $uploadPath = realpath(WEB_PATH) . '/uploads';
        $uploadUrl  = config('myconfig.host_url') . 'uploads/';
        $info = $file->validate(['size'=>$size,'ext'=>$ext])->move( $uploadPath );
        if($info){
            $url = $uploadUrl . $info->getSaveName();
            $uploadFile = $uploadPath . '/' . $info->getSaveName();

            if(strpos($url , "\\") > 0){
                $url = str_replace("\\" , '/' , $url);
            }
            return json(['error' => 0, 'url' => $url]);
        }else{
            return json(['error' => 1, 'message' => $file->getError()]);
        }
    }
    /**
     * 文件管理
     */
    public function filemanager(){
        $uploadPath = realpath(WEB_PATH) . '/uploads/';
        $uploadUrl  = config('myconfig.host_url') . 'uploads/';
        $ext_arr = ['gif','jpg','jpeg','png','bmp'];
        $current_path = $uploadPath;
        $current_url = $uploadUrl;
        $current_dir_path = '';
        $moveup_dir_path = '';
        if(($path = input('path') )!== ''){
            $current_path .= $path;
            $current_url .= $path;
            $current_dir_path = $path;
            $moveup_dir_path = preg_replace('/(.*?)[^\/]+\/$/', '$1', $current_dir_path);
        }
        $file_list = [];
        if ($handle = opendir($current_path)) {
            $i = 0;
            while (false !== ($filename = readdir($handle))) {
                if ($filename{0} == '.') continue;
                $file = $current_path . $filename;
                if (is_dir($file)) {
                    $file_list[$i]['is_dir'] = true; //是否文件夹
                    $file_list[$i]['has_file'] = (count(scandir($file)) > 2); //文件夹是否包含文件
                    $file_list[$i]['filesize'] = 0; //文件大小
                    $file_list[$i]['is_photo'] = false; //是否图片
                    $file_list[$i]['filetype'] = ''; //文件类别，用扩展名判断
                } else {
                    $file_list[$i]['is_dir'] = false;
                    $file_list[$i]['has_file'] = false;
                    $file_list[$i]['filesize'] = filesize($file);
                    $file_list[$i]['dir_path'] = '';
                    $file_ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
                    $file_list[$i]['is_photo'] = in_array($file_ext, $ext_arr);
                    $file_list[$i]['filetype'] = $file_ext;
                }
                $file_list[$i]['filename'] = $filename; //文件名，包含扩展名
                $file_list[$i]['datetime'] = date('Y-m-d H:i:s', filemtime($file)); //文件最后修改时间
                $i++;
            }
            closedir($handle);
        }
        $data = [
            'moveup_dir_path' => $moveup_dir_path,
            'current_dir_path' => $current_dir_path,
            'current_url' => $current_url,
            'total_count' => count($file_list),
            'file_list' => $file_list
        ];
        return json($data);
    }

    public function filedel(){
        $uploadPath = realpath(WEB_PATH) . '/';
        $file = input('CrrentDelUrl');
        $isDir = input('CurrentIsDir');
        $filepath = $uploadPath . $file;
        try{
            if($isDir == 'D'){
                rmdir($filepath);
            }else{
                unlink($filepath);
            }
            return json(['status'=>'y' , 'info'=>'删除成功']);
        }catch(Exception $e){
            return json(['status'=>'n' , 'info'=>$e->getMessage()]);
        }
    }
}